import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 * Part of the expert-mode steps. The user is prompted
 * for their name and program title.
 * 
 * @author James Stanier
 */
public class TitleGUI implements ActionListener {
	
	String title;
	String author;

	JFrame prevWindow;
	JFrame window;
	ExpertGUI settings;
	
	JTextArea titleEntry;
	JTextArea nameEntry;
	
	JButton backButton;
	JButton nextButton;
	
	/**
	 * Constructor.
	 * @param e The previous window, ExpertGUI.
	 * @param j The JFrame window belonging to the ExpertGUI.
	 */
	public TitleGUI(ExpertGUI e, JFrame j) {
		settings = e;
		prevWindow = j;
		titleScreen();
	}
	
	/**
	 * Configures and draws the screen.
	 */
	public void titleScreen() {
		window = new JFrame("JavaLiN - Enter title details");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,300);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);
		
		JPanel intro = new JPanel();
		intro.setLayout(new BorderLayout());
		
		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		intro.add(logo, BorderLayout.NORTH);
		
		// Intro text
		JLabel intr = new JLabel("<HTML>Please enter the program title and your name.</HTML>");
		intr.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		intr.setHorizontalAlignment(JLabel.CENTER);
		intro.add(intr, BorderLayout.SOUTH);
		
		
		// Panel for text boxes
		JPanel top = new JPanel();
		top.setLayout(new GridLayout(2,2));
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		JLabel namePrompt = new JLabel("<HTML> Your name:");
		namePrompt.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		namePrompt.setHorizontalAlignment(JLabel.CENTER);
		top.add(namePrompt);
		
		nameEntry = new JTextArea();
		nameEntry.setLineWrap(true);
		nameEntry.setWrapStyleWord(true);
		JScrollPane scrollingName = new JScrollPane(nameEntry);
		//scrollingName.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		top.add(scrollingName);
		
		JLabel titlePrompt = new JLabel("<HTML> Program title:");
		titlePrompt.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		titlePrompt.setHorizontalAlignment(JLabel.CENTER);
		top.add(titlePrompt);
		
		titleEntry = new JTextArea();
		titleEntry.setLineWrap(true);
		titleEntry.setWrapStyleWord(true);
		JScrollPane scrollingTitle = new JScrollPane(titleEntry);
		//scrollingTitle.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		top.add(scrollingTitle);
		
		// Buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Create report");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);
		
		window.add("North", intro);
		window.add("Center", top);
		window.add("South", navButtons);
		window.validate();
		window.setVisible(true);
	}
	
	/**
	 * Get the current title.
	 * @return The title as a String.
	 */
	public String getTitle() {
		return title;
	}
	
	/**
	 * Get the current author name.
	 * @return The author as a String.
	 */
	public String getAuthor() {
		return author;
	}
	
	/**
	 * Return the previous window (used for accessing settings).
	 * @return The ExpertGUI associated.
	 */
	public ExpertGUI getExpertGUI() {
		return settings;
	}
	
	/**
	 * Actions to be taken upon component interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(backButton)) {
			clearSettings();
			window.setVisible(false);
			prevWindow.setVisible(true);
		}
		else if(x.equals(nextButton)) {
			title = titleEntry.getText();
			author = nameEntry.getText();
			
			OutputHandler oh = new OutputHandler(this, settings.getInputFiles(), window);
		}
	}
	
	/**
	 * Clear the settings relevant to this screen.
	 */
	public void clearSettings() {
		titleEntry.setText("");
		nameEntry.setText("");
		title = "";
		author = "";
	}
}
